<?php
/**
 * 单服务器缓存操作类
 * @author rose
 */
class Cache{
	
	public $url = 'localhost';
	public $port = '11211';
	private $cache;
	
	public function __construct(){
		$this->cache = new Memcache;
		$this->cache->connect ($this->url, $this->port);
	}
	
	/**
	 * @return the $cache
	 */
	public function getCache() {
		return $this->cache;
	}

	/**
	 * @param field_type $cache
	 */
	public function setCache($cache) {
		$this->cache = $cache;
	}
	
	public function set($key, $value, $times=0){
		if (strlen($key) == 0) return false;
		$result = $this->cache->set($key, $value, 0, $times);
		return $result;
	}
	
	public function get($key){
		if (strlen($key) == 0) return;
		$value = $this->cache->get($key);
		return $value;
	}
	public function close(){
		return $this->cache->close();
	}
	public function delete($key){
		if (strlen($key) == 0) return false;
		$result = $this->cache->delete($key);
		return $result;
	}
	public function update($key, $value, $times=0){
		if (strlen($key) == 0) return false;
		$this->delete($key);
		return $this->set($key, $value, $times);
	}
	public function flush(){
		$result = $this->cache->flush();
		return $result;
	}
}


